//
//  MTChatSendGiftVew.swift
//  MT
//
//  Created by ooops on 2019/12/22.
//  Copyright © 2019 张强. All rights reserved.
//

import UIKit

@objc protocol MTChatSendGiftVewDelegate: NSObjectProtocol {
    /// 点击了送礼物按钮
    @objc optional func didSelectedSendGift(_ giftView: MTChatSendGiftVew)
    /// 点击了加好友按钮
    @objc optional func didSelectedaddFriend(_ giftView: MTChatSendGiftVew, user: JMSGUser)
}

class MTChatSendGiftVew: UIView {
    
    private var user: JMSGUser?
    
    weak var delegate: MTChatSendGiftVewDelegate?

    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
        makeConstraint()
    }
    
    convenience init(imUser: JMSGUser?) {
        self.init(frame: CGRect(x: 0, y: 0, width: ScreenWidth, height: 56))
        self.user = imUser
    }
    
    private func setupUI() {
        addSubview(addFriend)
        addSubview(sendGift)
    }
    
    private func makeConstraint() {
        addFriend.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(6)
            make.left.equalToSuperview().offset(15)
            make.height.equalTo(44)
        }
        
        sendGift.snp.makeConstraints { (make) in
            make.centerY.equalTo(addFriend)
            make.left.equalTo(addFriend.snp.right).offset(7)
            make.right.equalToSuperview().offset(-15)
            make.height.equalTo(44)
            make.width.equalTo(addFriend)
        }
    }
    
    lazy var addFriend: UIButton = {
        let btn = UIButton(type: .custom)
        btn.setTitle("加好友", for: .normal)
        btn.setTitleColor(UIColor.common.x161616, for: .normal)
        btn.backgroundColor = UIColor.theme.main
        btn.layer.cornerRadius = 22
        btn.layer.masksToBounds = true
        btn.addTarget(self, action: #selector(addFriend(_:)), for: .touchUpInside)
        return btn
    }()
    
    lazy var sendGift: UIButton = {
        let btn = UIButton(type: .custom)
        btn.setTitle("送礼物", for: .normal)
        btn.setTitleColor(UIColor.text.xf3f3f3, for: .normal)
        btn.backgroundColor = UIColor.im.xfd387c
        btn.layer.cornerRadius = 22
        btn.layer.masksToBounds = true
        btn.addTarget(self, action: #selector(sendGift(_:)), for: .touchUpInside)
        return btn
    }()
    
    @objc private func addFriend(_ sender: UIButton) {
        guard let user = user else {
            return
        }
        
        self.delegate?.didSelectedaddFriend?(self, user: user)
        
//        JMSGFriendManager.sendInvitationRequest(withUsername: user.username, appKey: user.appKey, reason: "我是\(JMSGUser.myInfo().nickname ?? "")") { (result, error) in
//            if error == nil {
//                let currentUser = JMSGUser.myInfo()
//                let info = JCVerificationInfo.create(username: user.username, nickname: user.nickname, appkey: user.appKey!, resaon: "我是\(JMSGUser.myInfo().nickname ?? "")", state: JCVerificationType.wait.rawValue, initiator: currentUser.username)
//                JCVerificationInfoDB.shareInstance.insertData(info)
//                NotificationCenter.default.post(name: Notification.Name(rawValue: kUpdateVerification), object: nil)
//                MBProgressHUD_JChat.show(text: "信息已发送给对方\n请等待回复", view: self.superview, 2)
//            } else {
//                MBProgressHUD_JChat.show(text: "\(String.errorAlert(error! as NSError))", view: self.superview)
//            }
//        }
        
    }
    
    @objc private func sendGift(_ sender: UIButton) {
        guard let user = user else {
            return
        }
        
        self.delegate?.didSelectedSendGift?(self)
        
        AlertManager.presentGiftView(isFriend: true, imUserName: user.username, didClickRechargeCallBack: { [weak self] in
            guard let weakSelf = self else { return }
            if UserDefaults.AppValidate.isValidate.storedBool == false {
                let c = MTPaymentViewController()
                weakSelf.viewController?.navigationController?.pushViewController(c, animated: true)
            } else {
                let c = MTInAppPurchasesViewController()
                weakSelf.viewController?.navigationController?.pushViewController(c, animated: true)
            }
            }, didClickSayHiCallBack: { (model, count) in
                
        }) {
            
        }
    }
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
    }

}
